/**
 * 
 */
package edu.cmu.aes.project1.app3;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * @author swasanta
 * 
 */
public class TransformationSample {
	public static void main(String[] args) {
		try {

			InputStream xslStream = ClassLoader
					.getSystemResourceAsStream("app3/howto.xsl");

//			InputStream xmlStream = ClassLoader
//					.getSystemResourceAsStream("app3/howto.xml");

			TransformerFactory tFactory = TransformerFactory.newInstance();

			Transformer transformer = tFactory
					.newTransformer(new javax.xml.transform.stream.StreamSource(
							xslStream));
			
			DOMSource domsource = parseXML("app3/howto.xml");
			
			transformer.transform(domsource, new javax.xml.transform.stream.StreamResult(
					new FileOutputStream("output/howto.html")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}

	public static DOMSource parseXML(String fileName) {
		DocumentBuilderFactory builderFactory = DocumentBuilderFactory
				.newInstance();

		try {
			InputStream xmlStream = ClassLoader.getSystemResourceAsStream(fileName);
			builderFactory.setNamespaceAware(true); // never forget this!
			DocumentBuilder builder = builderFactory.newDocumentBuilder();
			Document document = builder.parse(xmlStream);
			
			XPathFactory xPathFactory = XPathFactory.newInstance();
			XPath xpath = xPathFactory.newXPath();
			XPathExpression expr = xpath.compile("/howto/topic[contains(title,'Java')]");
			Object result = expr.evaluate(document, XPathConstants.NODESET);
			NodeList nodes = (NodeList) result;
//			for (int i = 0; i < nodes.getLength(); i++) {
//				Node node = nodes.item(i);
//			    System.out.println(node.getTextContent()); 
//			}
			
			if(nodes.getLength()>0){
				Document newDocument = builder.newDocument();
				Element element = newDocument.createElement("howto");
				for (int i = 0; i < nodes.getLength(); i++) {
					Node node = nodes.item(i);
				    element.appendChild(newDocument.adoptNode(node.cloneNode(true)));
				    
				}
				System.out.println(element.getTextContent());
				return new DOMSource(element);
			}
			
			
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return new DOMSource();
	}
}
